package com.meow.action.admin.basic;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.meow.dao.entity.basic.Users;
import com.meow.dao.mapper.basic.UsersDao;
import com.meow.util.AjaxOut;
import com.meow.util.EntityReflect;

@Controller
@RequestMapping("/admin/basic/LoginAction")
public class LoginAction {
	
	@Autowired
	private UsersDao usersDao;
	
	private static final long serialVersionUID = 9064995136509388439L;
	private Logger logger = Logger.getLogger(LoginAction.class);
	
	@RequestMapping("login.action")
	public void login(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			Users userEty = (Users) EntityReflect.createObjectFromRequest(request, Users.class);
			
			List list = usersDao.selectByEntity(userEty);
			if(list.size() == 1) {
				userEty = (Users) list.get(0);
				request.getSession().setAttribute("UserEty", userEty);
				obj.put("result","success");
			}
			else {
				obj.put("result","fail");
			}
		}
		catch (Exception e) {
			logger.error("", e);
			obj.put("result","error");
			obj.put("result",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}
	
	@RequestMapping("logout.action")
	public void logout(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			request.getSession().removeAttribute("UserEty");
			request.getSession().invalidate();
			obj.put("result","success");
		}
		catch (Exception e) {
			logger.error("", e);
			obj.put("result","error");
			obj.put("result",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}
}
